package com.zhouk.ams.wx.jump;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Create By Zhouke on 2018/01/12
 */
public class JumpHelper {

    //棋子的宽度，从截屏中量取，自行调节
    private final int halmaBodyWidth = 74;
    //二分之一的棋子底座高度
    private final int halfBaseBoardHeight = 20;

    private int gameScoreBottomY = 0;
    private int gameScoreBottomX = 0;
    private RGBInfo rgbInfo = new RGBInfo();


    //游戏截屏里的两个跳板的中点坐标，主要用来计算角度，可依据实际的截屏计算，计算XY的比例
    private final int boardX1 = 813;

    private final int boardY1 = 1122;

    private final int boardX2 = 310;

    private final int boardY2 = 813;

    //1.识别小人位置
    public void getPeopleLocation(File image) throws IOException {
        BufferedImage bufferedImage = ImageIO.read(image);
        int height = bufferedImage.getHeight();
        int width = bufferedImage.getWidth();

        int halmaXSum = 0;
        int halmaXCount = 0;
        int halmaYMax = 0;

        int boardX = 0;
        int boardY = 0;
        for (int y = gameScoreBottomY; y < height; y++) {
            for (int x = gameScoreBottomX; x < width; x++) {
                processRGBInfo(bufferedImage, x, y);
                int rValue = this.rgbInfo.getRValue();
                int gValue = this.rgbInfo.getGValue();
                int bValue = this.rgbInfo.getBValue();

                //根据RGB的颜色来识别棋子的位置，
                if (rValue > 50 && rValue < 60 && gValue > 53 && gValue < 63 && bValue > 95 && bValue < 110) {
                    halmaXSum += x;
                    halmaXCount++;
                    //棋子底行的Y坐标值
                    halmaYMax = y > halmaYMax ? y : halmaYMax;
                    //System.out.println("棋子底行Y坐标：" + y);
                }
            }
        }

        if (halmaXSum != 0 && halmaXCount != 0) {
            //棋子底行的X坐标值
            int halmaX = halmaXSum / halmaXCount;
            //上移棋子底盘高度的一半
            int halmaY = halmaYMax - halfBaseBoardHeight;
            //从gameScoreBottomY开始
            for (int y = gameScoreBottomY; y < height; y++) {
                processRGBInfo(bufferedImage, 0, y);
                int lastPixelR = this.rgbInfo.getRValue();
                int lastPixelG = this.rgbInfo.getGValue();
                int lastPixelB = this.rgbInfo.getBValue();
                //只要计算出来的boardX的值大于0，就表示下个跳板的中心坐标X值取到了。
                if (boardX > 0) {
                    break;
                }
                int boardXSum = 0;
                int boardXCount = 0;
                for (int x = 0; x < width; x++) {
                    processRGBInfo(bufferedImage, x, y);
                    int pixelR = this.rgbInfo.getRValue();
                    int pixelG = this.rgbInfo.getGValue();
                    int pixelB = this.rgbInfo.getBValue();
                    //处理棋子头部比下一个跳板还高的情况
                    if (Math.abs(x - halmaX) < halmaBodyWidth) {
                        continue;
                    }

                    //从上往下逐行扫描至下一个跳板的顶点位置，下个跳板可能为圆形，也可能为方框，取多个点，求平均值
                    if ((Math.abs(pixelR - lastPixelR) + Math.abs(pixelG - lastPixelG) + Math.abs(pixelB - lastPixelB)) > 10) {
                        boardXSum += x;
                        boardXCount++;
                    }
                }

                if (boardXSum > 0) {
                    boardX = boardXSum / boardXCount;
                }
            }

            //按实际的角度来算，找到接近下一个 board 中心的坐标
            boardY = (int) (halmaY - Math.abs(boardX - halmaX) * Math.abs(boardY1 - boardY2)
                    / Math.abs(boardX1 - boardX2));
            if (boardX > 0 && boardY > 0) {
                int[] result = new int[4];
                //棋子的X坐标
                result[0] = halmaX;
                //棋子的Y坐标
                result[1] = halmaY;
                //下一块跳板的X坐标
                result[2] = boardX;
                //下一块跳板的Y坐标
                result[3] = boardY;

                System.out.println(halmaX);
                System.out.println(halmaY);
                System.out.println(boardX);
                System.out.println(boardY);

                //return result;
            }

            Graphics2D graphics = bufferedImage.createGraphics();
//            graphics.drawImage(
//                    bufferedImage.getScaledInstance(bufferedImage.getWidth(null),
//                            bufferedImage.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
//                    null);

            //graphics.fillOval(1, 1, halmaX-1 , halmaX-1);
            //graphics.fillOval(boardX, boardY, 5 , 5);
            graphics.setStroke(new BasicStroke(5f));
            //graphics.drawLine(halmaX-2,halmaY-2,halmaX+2,halmaY+2);
            //graphics.drawLine(boardX-2,boardY-2,boardX+2,boardY+2);
            graphics.drawString("人", halmaX, halmaY);
            graphics.setColor(Color.BLACK);
            graphics.drawString("方块", boardX, boardY);
            graphics.dispose();
            //String file = new Random().nextInt(99999999) + ".jpg";
            OutputStream os = new FileOutputStream("D:/Desktop/qrtest" + "/" + "mark_location.jpg");
            ImageIO.write(bufferedImage, "JPG", os);
        }
    }


    //2.识别下一方块中心店位置
    //3.模拟跳跃


    private void processRGBInfo(BufferedImage bufferedImage, int x, int y) {
        this.rgbInfo.reset();
        int pixel = bufferedImage.getRGB(x, y);
        //转换为RGB数字
        this.rgbInfo.setRValue((pixel & 0xff0000) >> 16);
        this.rgbInfo.setGValue((pixel & 0xff00) >> 8);
        this.rgbInfo.setBValue((pixel & 0xff));
    }
}
